<?php
/********视频接口，奥点云http://www.aodianyun.com/aodianyun_doc/83****/
namespace Common\Org\Video\Driver;
class Aodianyun {
	function __construct($param) {
		$this->access_id = '967065988252';
		$this->access_key = 'D06j9R2aoQMFKi7K3VS7LE7lH88aA9cG';
		$this->upload_api = 'http://upload.dvr.aodianyun.com/v2'; //上传接口地址
	}

	/********视频列表**********/
	public function VodList($param) {
		$url = "http://openapi.aodianyun.com/v2/VOD.GetUploadVodList";
		$post = array(
			'access_id' => $this->access_id,
			'access_key' => $this->access_key,
			'num' => $param['num'] ? $param['num'] : "30",
			'page' => $param['page'] ? $param['page'] : 1,
			'uptime' => $param['uptime'], //上传时间
			'title' => $param['title'], //标题
			'url' => $param['url'],
		);
		$post = json_encode($post);
		$content = json_decode(myCurl($url, $post), true);
		$data = $content['List'];
		return $data;
	}

	/****视频信息***/
	public function video_info() {

	}

	/******上传视频*****/
	public function upload($param) {
		@set_time_limit(0);
		$access_id = $this->access_id; //填入access_id
		$access_key = $this->access_key; //填入access_key
		//$fileName = 'demo.flv';//上传的文件路径
		$fileName = $param['fileName'];
		$fileContent = $param['fileContent'];
		$key = md5(json_encode(array(
			'fileName' => $fileName,
			'session_id' => session_id(),
		))); //缓存键值
		S($key, 0); //缓存键值
		$offset = 0;
		$flag = 0;
		$sliceSize = 1 * 1024 * 1024; //分片大小
		$partNum = 1;
		$fileSize = filesize($fileContent);
		while ($flag != 1) {
			$dataSize = 0;
			//计算本次上传分片的大小
			if ($offset + $sliceSize > $fileSize) {
				$dataSize = $fileSize - $offset;
			} else {
				$dataSize = $sliceSize;
			}
			$part = urlencode(base64_encode(file_get_contents($fileContent, false, NULL, $offset, $dataSize)));
			$param = array(
				'access_id' => $access_id,
				'access_key' => $access_key,
				'fileName' => $fileName,
				'part' => $part,
				'partNum' => $partNum,
			);

			$res = myCurl($this->upload_api . '/DVR.UploadPart', 'parameter=' . json_encode($param));
			if (!empty($res) && $flag == 0) {
				$res = json_decode($res, true);
				if ($res['Flag'] == 100) {
					$partNum++;
					$offset += $dataSize;
				}
			}
			$progress = $offset / $fileSize; //上传进度
			S($key, $progress); //上传进度结束
			if ($offset == $fileSize) {
				$flag = 1;
				S($key, 1); //上传进度结束
				$complete_param = array('fileName' => $param['fileName']);
				return $this->UploadComplete($complete_param);
			}
		}
	}

	/*****上传进度***/
	public function UploadComplete($param) {
		$access_id = $this->access_id; //填入access_id
		$access_key = $this->access_key; //填入access_key
		$param = array(
			'access_id' => $access_id,
			'access_key' => $access_key,
			'fileName' => $param['fileName'],
		);
		$r = json_decode(myCurl($this->upload_api . '/DVR.UploadComplete', 'parameter=' . json_encode($param)), true);
		$data = array(
			'uniqid' => $r['location'],
			'filename' => $r['fileName'],
			'data' => $r,
			'type' => 'Aodianyun',
		);
		return $data;
	}

	/*****上传进度****/
	public function progress($param) {
		$access_id = $this->access_id; //填入access_id
		$access_key = $this->access_key; //填入access_key
		$param = array(
			'fileName' => $param['fileName'],
			'session_id' => session_id(),
		);
		//dump($param);
		$key = md5(json_encode($param));
		$progress = intval(S($key) * 100); //上传进度结束
		return $progress;
	}

}
